package by.averianov.discounta.webapp.page.homepage;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.ResourceModel;

import by.averianov.discounta.services.EmailSender;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.create.CreateUser;
import by.averianov.discounta.webapp.page.loginpage.LoginPage;
import by.averianov.discounta.webapp.page.user.addcard.AddCard;

public class HomePage extends BaseLayout {
	
	public HomePage() {
		super(null);
		super.currentMenu = "home";
	}
	
	@Inject
	public UserService userService;
	
	@Inject
	public EmailSender emailSender;
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		add(new Label("labelAbout", getLabelAbout()));
		
		Link buttonCreateUser = new Link("buttonCreateUser"){
			@Override
			public void onClick() {
				//BasicAuthenticationSession.get()
				if (AuthenticatedWebSession.get().isSignedIn()) {
					HomePage page = new HomePage();
					page.info(new ResourceModel("p.login.youhavealogin").getObject());
					setResponsePage(page);
					//info(new ResourceModel("p.login.youhavealogin").getObject());
					//setResponsePage(Application.get().getHomePage());					
				} else {
					setResponsePage(new CreateUser(null, null, new PageCreator() {
	
						@Override
						public Page createPage() {
							return new HomePage();
						}
					}));
				}
			}
		};
		
		buttonCreateUser.add(new Label("labelCreateUser", getButtonCreateUser()));
		add(buttonCreateUser);
		
		add(new Label("labelCreateUserText", getTextButtonCreateUser()));
		
		Link buttonLogin = new Link("buttonLogin"){
			@Override
			public void onClick() {
				if (AuthenticatedWebSession.get().isSignedIn()) {
					HomePage page = new HomePage();
					page.info(new ResourceModel("p.login.youareloggedin").getObject());
					setResponsePage(page);
					//info(new ResourceModel("p.login.youhavealogin").getObject());
					//setResponsePage(Application.get().getHomePage());
				} else {
					setResponsePage(new LoginPage(null));
				/*setResponsePage(new LoginPage(new PageCreator() {

					@Override
					public Page createPage() {
						return new HomePage();
					}
				}));*/
				}
			}
		};
		buttonLogin.add(new Label("labelLogin", getMenuLogin()));
		add(buttonLogin);
		add(new Label("labelLoginText", getTextButtonLogin()));
				
		Link buttonAddCard = new Link("buttonAddCard"){
			@Override
			public void onClick() {	
				
				if (!AuthenticatedWebSession.get().isSignedIn()) {
					HomePage page = new HomePage();
					page.error(new ResourceModel("p.error.nouserlogin").getObject());
					setResponsePage(page);
					//info(new ResourceModel("p.login.youhavealogin").getObject());
					//setResponsePage(Application.get().getHomePage());
				} else {
					setResponsePage(new AddCard( new PageCreator() {
	
						@Override
						public Page createPage() {
							return new HomePage();
						}
					}));
				}
			}
		};
		buttonAddCard.add(new Label("labelAddCard", getButtonAddCard()));
		add(buttonAddCard);
		add(new Label("labelAddCardText", getTextButtonAddCard()));
	}

}
